/*
 * RFTXSenderModule.cpp
 *
 *  Created on: 21/02/2012
 *      Author: xavi
 */
#include "HardwareSerial.h"
#include "RFSenderModule.h"
#include "VirtualWire.h"
#include <Streaming.h>
extern HardwareSerial Serial;
//int portTXVirtualWire=2;
int STATE_WAIT = 0;
int STATE_LISTEN = 1;
int STATE_READY = 2;
char txbuf[RFSenderModule::BUFFER_SIZE];
RFSenderModule::RFSenderModule(String srcId,int nBPS,int portTXVirtualWire,int initialState):SenderModule() {
	port=portTXVirtualWire;//port to send
	sourceId=srcId; // whoami
	state = initialState;
	//VW
	vw_setup(nBPS); // Bits per sec
	vw_set_tx_pin(port);
	init=true;

}

RFSenderModule::~RFSenderModule() {
	// TODO Auto-generated destructor stub
}

void RFSenderModule::sendMessage(Message& message){
	if (state==STATE_WAIT) {
		String s = message.getContent();
		if (s!=NULL) {
	//		memset(txbuf, '\0', sizeof(txbuf));
			for (int i=0;i<strlen(txbuf);i++) {
				txbuf[i]='\0';
			}
			s.toCharArray(txbuf,RFSenderModule::BUFFER_SIZE);
//					.getBytes((unsigned char *)txbuf,50,0);
			Serial << txbuf<< endl;
			vw_send((uint8_t *) txbuf, strlen(txbuf));
			vw_wait_tx();
		}
		delay(500);
	}
}
